package com.hook3.pianke.activity;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.hook3.pianke.R;
import com.hook3.pianke.domain.TopicHotInfo;

import java.util.ArrayList;
import java.util.List;


/**
 * 社区页面
 * Created by 柴孟鑫on
 *
 */

public class CommunityActivity {

    private Activity activity;
    private Toolbar toolbar;  //标题
    private FrameLayout frameLayout;//实现View;
    private View mTitlecomm;
    private View mCommContent;

    private static final String []titles = {"最新","最热"};
    private ViewPager viewpager;
    private List<TopicHotInfo> commInfos;
    private List<View>ll;





    public CommunityActivity(Activity activity, Toolbar toolbar, FrameLayout frameLayout) {
        this.activity = activity;
        this.toolbar = toolbar;
        this.frameLayout = frameLayout;
        init();
    }

    /**
     * 实例化自己要实现的内容
     */
    public void init()  {
        toolbar.setTitle("");

        //设置标题
        mTitlecomm =  View.inflate(activity, R.layout.community_title, null);
        mTitlecomm.setOnClickListener(new MyOnClickListener());

        toolbar.addView(mTitlecomm);
        toolbar.setTag(mTitlecomm);
        mCommContent = View.inflate(activity,R.layout.community_context,null);
        ll = new ArrayList<>();
        viewpager = (ViewPager) mCommContent.findViewById(R.id.viewpager);

        for(int i = 0;i<titles.length;i++){
            TextView textView = new TextView(activity);
            textView.setText(titles[i]);
            textView.setTextSize(20);
            ll.add(textView);


        }
        viewpager.setAdapter(new MyCommPaerAdapter());


        frameLayout.addView(mCommContent);


    }

    /**
     * 设置点击
     */
    class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            showPopupWindow(v);

        }
    }

    /**
     * 设置pupwindow
     * @param v
     */
    private PopupWindow popupWindow;

    private void showPopupWindow(View v) {
        View contentView = LayoutInflater.from(activity).inflate(
                R.layout.pop_window, null);
        // 设置按钮的点击事件
        Button button1 = (Button) contentView.findViewById(R.id.btn_pop1);
        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(activity, "进入话题页面",
                        Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });
        Button button2 = (Button) contentView.findViewById(R.id.btn_pop2);
        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(activity, "进入小组页面",
                        Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });
        Button button3 = (Button) contentView.findViewById(R.id.btn_pop3);
        button3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(activity, "进入我的页面",
                        Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });

         popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.i("mengdd", "onTouch : ");

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        // 设置好参数之后再show
        popupWindow.showAsDropDown(v,0,toolbar.getHeight()-v.getHeight()-(toolbar.getHeight()/2-v.getHeight()/2));
    }


    private class MyCommPaerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return ll.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(ll.get(position));
            return ll.get(position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(ll.get(position));
        }
    }
}